package com.wuming.caiwu.controller;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.CommonResult;
import com.wuming.caiwu.model.DmsOrder;
import com.wuming.caiwu.model.FinanceReport;
import com.wuming.caiwu.service.DmsOrderService;
import com.wuming.caiwu.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/finance")
public class FinanceReportController extends BaseController {

    @Autowired
    private DmsOrderService orderService;

    @RequestMapping(value = "/manageFinanceReport", method = RequestMethod.GET)
    public String manageOrder() {
        return "/order/manageFinanceReport";
    }


    @ResponseBody
    @RequestMapping(value = "/getFinanceReport", method = RequestMethod.GET)
    public CommonResult getFinanceReport(@RequestParam(required = false) Long storeId,
                             @RequestParam(required = false) String startDate,
                             @RequestParam(required = false) String endDate,
                             @RequestParam(defaultValue = "0") Integer payType,
                             @RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize){
        try {
            if(StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
                Calendar current = Calendar.getInstance();
                endDate = DateUtil.dateToString(current.getTime(),"yyyy-MM-dd");
                startDate = DateUtil.dateToString(DateUtil.addDays(current.getTime(),-10),"yyyy-MM-dd");
            }
            List<FinanceReport> orders = orderService.getFinanceReportByPage(storeId,startDate,endDate,payType,pageIndex, pageSize);
            int total = orderService.getFinanceReportTotal(storeId,startDate,endDate,payType);
            if(total == 0){
                return CommonResult.createSuccessResult(null, total, "查询订单信息成功.");
            }else{
                return CommonResult.createSuccessResult(orders, total, "查询订单信息成功.");
            }
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询订单信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询订单信息失败.");
        }
    }

//    @ResponseBody
//    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
//    public CommonResult delete(@PathVariable Integer id) {
//        logger.info("准备删除店铺信息ID:{}", id);
//        try {
//            storeService.removeQsgStoreById(id);
//            return CommonResult.createSuccessResult("删除店铺信息成功.");
//        } catch (BusinessException be) {
//            logger.error("发生业务异常,异常信息:{}", be.getMessage());
//            return CommonResult.createFailureResult("删除店铺信息失败.");
//        } catch (Exception e) {
//            logger.error("发生系统异常,异常信息:{}", e.getMessage());
//            return CommonResult.createFailureResult("删除店铺信息失败.");
//        }
//    }

}
